﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mamnontuoitho7.AppCode;
using System.Data;
using System.Text.RegularExpressions;

namespace mamnontuoitho7.module
{
    public partial class KidHealInfoView : System.Web.UI.Page
    {

        protected Kid _kid = new Kid();
        protected Teacher _teacher = new Teacher();
        protected General _general = new General();
        private KidWall _kidWall = new KidWall();
        private KidHeal _kidHeal = new KidHeal();
        protected DataRow _dtrKid;

        protected String _kidID = "";
        protected String _checkValidKid = "false";

        protected DataTable _dttKidHeal;
        protected int _pageflagH;
        protected int _currentbarH = 1;

        protected int _totalPagesH;
        protected int _totalHealNumber;
        protected int _totalBarsH;

        protected int _beginPageH;// dung bên trang giao diện
        protected int _endPageH;// dung bên trang giao diện

        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["memberID"].ToString() != "" && (Session["role"].ToString() == "3" || Session["role"].ToString() == "4"))
            {
                    try
                    {
                        if (Request.QueryString["KidID"] != null && Request.QueryString["KidID"].ToString() != "")
                        {
                            _kidID = Request.QueryString["KidID"].ToString();


                            //Delete Heal info of kid
                            if (Request.Form["hidKidInformationID"] != null && Request.Form["hidKidInformationID"].ToString() != "")
                            {
                                _kidHeal.DeleteKidHealInfo(Request.Form["hidKidInformationID"].ToString());
                            }

                            Match match = Regex.Match(_kidID, @"([0-9])$");
                                //If kidID is greater than 6 character
                                if (_kidID.ToString().Length > 6 || !match.Success)
                                {
                                    return;
                                }

                                //Check Teacher or Parent view kid details
                                if (Session["role"].ToString() == "3")
                                {
                                    _dtrKid = _kid.GetKidInformationOfClass(_kidID, Session["classID"].ToString());
                                }
                                else if (Session["role"].ToString() == "4")
                                {
                                    _dtrKid = _kid.GetKidInformationOfParent(_kidID, Session["memberID"].ToString());
                                }

                                if (_dtrKid == null)
                                {
                                    _checkValidKid = "false";
                                    return;
                                }
                                _checkValidKid = "true";

                                /////////////////////START PAGING FOR KID HEAL
                                _totalHealNumber = _kidHeal.TotalHealOfOneKid(_kidID).GetValueOrDefault();
                                _totalPagesH = _general.Divide(_totalHealNumber, ConstantClass._pageStepHeal);
                                _totalBarsH = _general.Divide(_totalPagesH, ConstantClass._barStepHeal);

                                if (Request.QueryString["pageH"] != null)// khi bấm vào các ô số
                                {
                                    //TabSelect(3);
                                    string temp = Request.QueryString["pageH"].ToString();
                                    Match match2 = Regex.Match(temp, @"([0-9])$", RegexOptions.IgnoreCase);

                                    if (temp.Length <= 4 && match2.Success)
                                    {
                                        _pageflagH = Convert.ToInt16(temp);// trang hiện hành
                                        if (_pageflagH <= _totalPagesH)
                                        {
                                            _currentbarH = _general.Divide(_pageflagH, ConstantClass._barStepHeal);
                                            Response.Cookies["CurrentBar"].Value = _currentbarH.ToString();
                                        }
                                        else
                                        {
                                            _pageflagH = 1;
                                            _currentbarH = 1;
                                            Response.Cookies["CurrentBar"].Value = _currentbarH.ToString();
                                        }
                                    }
                                }
                                if (Request.QueryString["pageH"] == null && Request.QueryString["aH"] == null)// khi mới vào trang
                                {
                                    _currentbarH = 1;
                                    _pageflagH = 1;
                                    Response.Cookies["CurrentBar"].Value = _currentbarH.ToString();
                                }

                                if (Request.QueryString["aH"] != null && Request.QueryString["aH"] == "1") // khi bấm vào ô >> (next)
                                {
                                    //TabSelect(3);
                                    _currentbarH = Int16.Parse(Request.Cookies["CurrentBar"].Value);
                                    if (_currentbarH < _totalBarsH)
                                    {
                                        _currentbarH += 1;
                                        Response.Cookies["CurrentBar"].Value = _currentbarH.ToString();
                                    }
                                    _pageflagH = (_currentbarH - 1) * ConstantClass._barStepHeal + 1;

                                }

                                if (Request.QueryString["aH"] != null && Request.QueryString["aH"] == "-1") // khi bấm vào ô << (previous)
                                {
                                    //TabSelect(3);
                                    _currentbarH = Int16.Parse(Request.Cookies["CurrentBar"].Value);
                                    if (_currentbarH > 1)
                                    {
                                        _currentbarH -= 1;
                                        Response.Cookies["CurrentBar"].Value = _currentbarH.ToString();
                                    }
                                    _pageflagH = (_currentbarH - 1) * ConstantClass._barStepHeal + 1;
                                }



                                _dttKidHeal = _kidHeal.GetHealPaged(_pageflagH, ConstantClass._pageStepHeal, _kidID);
                                _beginPageH = (_currentbarH - 1) * ConstantClass._barStepHeal + 1;
                                _endPageH = _currentbarH * ConstantClass._barStepHeal;

                                if (_endPageH > _totalPagesH)
                                {
                                    _endPageH = _totalPagesH;
                                }

                            
                        }
                        else
                        {
                            Response.Redirect(ConstantClass._errorLoginPage);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        Response.Redirect(ConstantClass._errorLoginPage);
                    }
            }
            else
            {
                Response.Redirect(ConstantClass._errorLoginPage);
            }


            Session["Common_articleTypeID"] = "1";
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (Request.QueryString["KidID"] != null && Request.QueryString["KidID"].ToString() != "")
                {
                    _kidID = Request.QueryString["KidID"].ToString();
                    bool check = _kidHeal.KidHealCreate(Session["memberID"].ToString(), _kidID, txtKidWeight.Text.Trim(), txtWeightNotice.Text.Trim(),
                                                            txtKidHeight.Text.Trim(), txtHeightNotice.Text.Trim(), txtResult.Text.Trim());

                    if (!check)
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Có lỗi hệ thống khi lưu thông tin.\n Xin vui lòng làm lại.')", true);
                    }
                    else
                    {
                        //Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Thông tin về sức khỏe của bé đã được lưu.')", true);
                        _dttKidHeal = _kidHeal.GetHealPaged(_pageflagH, ConstantClass._pageStepHeal, _kidID);
                        ResetInformation();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Response.Redirect(ConstantClass._errorLoginPage);
            }
        }


        protected void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                ResetInformation();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Response.Redirect(ConstantClass._errorLoginPage);
            }
        }

        private void ResetInformation()
        {
            txtKidHeight.Text = "";
            txtKidWeight.Text = "";
            txtHeightNotice.Text = "";
            txtWeightNotice.Text = "";
            txtResult.Text = "";
        }
    }
}